// pages/user/evaluate/evaluate.js
var app = getApp();
var getImgarr = [];//需要过度展示图片
var upimgarr = [];//上传七牛云成功后返给后台的图片
var uping = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    starIndex: 0,
    starImg1: '../../../images/star-2.png',
    starImg2: '../../../images/star-1.png',
    labels: [],
    labelIndexs:[],
    labels_ids: [],
    evaluate_content:'',
    imgArr:[],
    work_order_id:'',
    detailData:{},
    uploadImgs: [],
    disabled: false, 
    noteMaxLen: 100,
    currentNoteLen: 0,
    imgUrl: [],
    upchoosebox: [],
    engineers_id: '',//工程人员ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      work_order_id: options.work_order_id
    })
    that.getapplyDataDetail();
    that.getEvaluateLabel();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    getImgarr = [];
    upimgarr = [];
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    getImgarr = [];
    upimgarr = [];
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 评价星级
   */
  starClick: function (e) {
    //console.log(e.currentTarget.dataset.star);
    this.setData({
      starIndex: e.currentTarget.dataset.star
    })
  },
  // 标签点击
  labelClick: function(e){
    //console.log(e.currentTarget.dataset.labelindex);
    var that = this;
    var index = e.currentTarget.dataset.labelindex;
    var label_id = e.currentTarget.dataset.label_id;
    var up = "labelIndexs["+index+"]";
    var labels_ids = that.data.labels_ids;
    if (this.data.labelIndexs[index] == undefined ||this.data.labelIndexs[index] == false){
      labels_ids.push(label_id);
      that.setData({
        [up]: true,
        labels_ids: labels_ids
      });
    } else{
      var s=999;
      labels_ids.forEach(function(v,k){
        if (label_id==v){
          s = k;
        }
      })
      labels_ids.splice(s, 1);
      that.setData({
        [up]: false,
        labels_ids: labels_ids
      });
    }
  },
  //输入其他评价
  inputOther: function(e){
    //console.log(e.detail.value);
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.noteMaxLen) return;

    this.setData({
      currentNoteLen: len, //当前字数 ,
      evaluate_content: e.detail.value
    });
  },
  // 选择图片
  chooseImg: function(){
    var that = this;
    var num = 3 - getImgarr.length;
    if (uping) {
      return false
    }
    wx.chooseImage({
      count: num, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        var tempFilePaths = res.tempFilePaths
        var url = that.data.imgUrl.concat(tempFilePaths);
        for (var k = 0; k < url.length; k++) {
          that.upImg(url[k]);//循环上传图片
        }
      }
    })

    // var that = this;
    // wx.chooseImage({
    //   count: 6,
    //   sizeType: ['compressed'],
    //   sourceType: ['album', 'camera'],
    //   success: function (res) {
    //     // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
    //     var tempFilePaths = res.tempFilePaths;
    //     var imgArr = [];
    //     that.setData({
    //       imgArr: imgArr
    //     })
    //     //console.log(tempFilePaths);
    //     for (var i = 0; i < tempFilePaths.length; i++) {
    //       that.upImg(tempFilePaths[i]);
    //     }
    //     that.setData({
    //       uploadImgs: tempFilePaths
    //     })
    //   }
    // })
    // //console.log(that.data.uploadImgs);
    
  },
  //图片上传
  upImg: function (tempFilePaths) {
    var that = this;
    wx.showLoading({
      title: '上传图片中',
    })
    uping = true;
    wx.uploadFile({
      url: app.globalData._url + 'Common/Common/upload',
      filePath: tempFilePaths,
      name: 'file',
      success: function (res) {
        wx.hideLoading();
        var data = JSON.parse(res.data);
        if (data.code == 1000) {
          getImgarr.push(data.data.all_url);
          upimgarr.push(data.data.url);
          that.setData({
            imgUrl: getImgarr,
            upchoosebox: upimgarr
          })
        } else {
          app.showToast("上传图片失败");
        }
      },
      complete: function () {
        uping = false
      }
    })
    // var that = this;
    // wx.uploadFile({
    //   url: app.globalData._url + 'Common/Common/upload', //仅为示例，非真实的接口地址
    //   filePath: tempFilePaths,
    //   name: 'file',
    //   success: function (res) {
    //     var res = JSON.parse(res.data);
    //     if (res.code == 1000) {
    //       //console.log(res)
    //       var imgUrl = res.data.url;
    //       var imgArr = that.data.imgArr;
    //       imgArr.push(imgUrl);
    //       that.setData({
    //         imgArr: imgArr
    //       })
    //       //console.log(that.data.imgArr);
    //     } else {
    //       app.showToast(res.data.message);
    //     }
    //     //do something imgArr
    //   }
    // })
  },
  //删除图片
  detailImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.detail;
    getImgarr.splice(index, 1);
    upimgarr.splice(index, 1);
    if (that.data.imgUrl.length == 0) {
      that.setData({
        imgUrl: getImgarr,
        upchoosebox: upimgarr
      })
    } else {
      that.setData({
        imgUrl: getImgarr,
        upchoosebox: upimgarr
      })
    }
  },
  // 提交
  Submit:function(){
    
   
      var that = this;
      that.setData({
        disabled:true
      })
      // console.log(that.data.detailData.repair_id)
      // console.log(that.data.work_order_id)
      // console.log(that.data.evaluate_content)
      // console.log(that.data.starIndex)
      // console.log(upimgarr)
      // console.log(that.data.labels_ids)
      app.ajax({
        url: 'User/Order/addUserEvaluation',
        method: "POST",
        data: {
          type:2,
          engineer_id: that.data.detailData.repair_id,
          work_order_id: that.data.work_order_id,
          evaluate_content:that.data.evaluate_content,
          evaluate_stars:that.data.starIndex,
          evaluate_image: upimgarr,
          label: that.data.labels_ids
        },
        success: function (res) {
          //console.log(res)
          that.setData({
            disabled: false
          })
         // console.log(res)
          if (res.data.code == 1000) {
            //console.log(res.data.data);
            app.showToast(res.data.message);
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];
            var prevPage = pages[pages.length - 2];
            //console.log(prevPage.data.isType);
            prevPage.setData({
              isType: true
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000);
            
            
           
          } else {
            app.showToast(res.data.message);
          }

        }
      })
  },
  //获取评价标签列表
  getEvaluateLabel() {
    var that = this;
    app.ajax({
      url: 'User/Order/getEvaluateLabel',
      method: "POST",
      data: {
        type: 2
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          //console.log(res.data.data);
          that.setData({
            labels: res.data.data
          })
        } else {
          //app.showToast(res.data.message);
        }
        
      }
    })
  },
  //查询订单详情
  getapplyDataDetail() {
    var that = this;
    var work_order_id = that.data.work_order_id;
    app.ajax({
      url: 'User/Work/workOrderDetail',
      method: "POST",
      data: {
        work_order_id: work_order_id
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          that.setData({
            detailData: res.data.data,
            engineers_id: res.data.data.engineers_id
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  // 工程人员投诉
  goComplaint: function () {
    var that = this;
    var engineers_id = that.data.engineers_id;
    wx.navigateTo({
      url: '../../complaint/complaint?comType=4&engineers_id=' + engineers_id,
    })

  }
})